//
//  LTWelcomeViewController.swift
//  微博08
//
//  Created by 123 on 16/3/4.
//  Copyright © 2016年 Tsr. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class LTWelcomeViewController: UIViewController {
    
    // 属性
    // iconView的底部约束
    var iconViewCons: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        
        setIcon()
        
        // 获取用户头像
        // 1.LTUserAccountViewModel的loadUserInfo,并准备闭包
        LTUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
                print("获取用户数据出错: \(error)")
                return
            }
            
            self.setIcon()
        }
    }
    
    /// 设置用户头像
    private func setIcon() {
        if let avatar_large = LTUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            let url = NSURL(string: avatar_large)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    // 界面出来后调用这个方法
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        moveAnimation()
    }
    
    // 准备UI控件
    private func prepareUI() {
        // 添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 设置约束
        // 背景
        bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        // 头像
        // make: 哪个view调用的就是哪个view
        // make.centerX: 表示约束iconImageView的centerX
        // equalTo: 相等
        // view: view参照的view
        // snp_centerX: 参照view的centerX属性,注意参照的属性需要添加 `snp_`
        
        // 当约束view的属性和参照view的属性相同的时候,参照的属性可以省略
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
        }
        // 底部
        iconView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-160)
        }
        
        // 大小
        iconView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        // messageLabel
        // 顶部(需要参照iconView)
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        // centerX
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
        }
    }
    
    // MARK: 动画方法
    private func moveAnimation() {
        
        // 更新iconView的底部约束
        iconView.snp_updateConstraints { (make) -> Void in
            // 要更新的约束
            make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.size.height - 160))
        }
        UIView.animateWithDuration(1.25, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 消息label的渐变动画
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        AppDelegate.outSwitchRootViewController(true)
                        
                })
                
        }
    }
    
    // MARK: 懒加载
    // 背景
    lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    // 头像
    lazy var iconView: UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 设置圆角
        iconView.layer.cornerRadius = 42.5
        iconView.clipsToBounds = true
        
        return iconView
    }()
    // label
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "欢迎归来"
        label.sizeToFit()
        label.alpha = 0
        
       return label
    }()
}
